#!/usr/bin/python

"""
__version__ = "$Revision: 1.10 $"
__date__ = "$Date: 2004/04/24 22:13:31 $"
"""

from PythonCard import model
import wx

class ConvApp(model.Background):
    def on_initialize(self,event):
        self.initializeList()

    # so how much to wrap and how much to leave raw wxPython?
    def initializeList(self):
        list = self.components.list
        
        list.InsertColumn(0, "From")
        list.InsertColumn(1, "To")
        list.InsertColumn(2, "Type")

        convData = {
            1: ("Celsius", "Fahrenheit", "Temperature"),
            
            2: ("Kilometers", "Miles", "Length"),
            3: ("Meters", "Feet", "Length"),
            4: ("Centimeters", "Inches", "Length"),
            
            5: ("Kilos", "Pounds", "Weight"),
            
            6: ("Liters", "US Gallons", "Liquid"),
            7: ("Centiliters", "US Ounces", "Liquid"),
            
            8: ("Kilometers/h", "Miles/h", "Speed"),
            9: ("Kilometers/h", "Knots", "Speed")}

        items = convData.items()

        for x in range(len(items)):
            key, data = items[x]
            list.InsertStringItem(x, data[0])
            list.SetStringItem(x, 0, data[0])
            list.SetStringItem(x, 1, data[1])
            list.SetStringItem(x, 2, data[2])
            list.SetItemData(x, key)

        list.SetColumnWidth(0, 70)
        list.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        list.SetColumnWidth(2, wx.LIST_AUTOSIZE)

        list.SetItemDataMap(convData)

        list.SetItemState(5, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)

        self.currentItem = 0
        

    def getColumnText(self, index, col):
        item = self.components.list.GetItem(index, col)
        return item.GetText()

    def on_list_select(self, event):
        self.currentItem = event.m_itemIndex
        #print "on_list_select: %s, %s, %s, %s\n" % (self.currentItem,
        #    self.components.list.GetItemText(self.currentItem),
        #    self.getColumnText(self.currentItem, 1),
        #    self.getColumnText(self.currentItem, 2))

    """
    def on_list_mouseDoubleClick(self, event):
        # event.target
        # is equivelant to
        # self.components.list
        print "on_list_mouseDoubleClick item %s\n" % self.components.list.GetItemText(self.currentItem)
        event.skip()

    def on_list_itemActivated(self, event):
        self.currentItem = event.m_itemIndex
        print "on_list_itemActivated: %s\n" % self.components.list.GetItemText(self.currentItem)
        item = self.components.list.GetItem(self.currentItem)
        print item.m_text, item.m_itemId, self.components.list.GetItemData(self.currentItem)

    def on_list_columnClick(self, event):
        print "on_list_columnClick: %d\n" % event.GetColumn()

    def on_list_keyDown(self, event):
        print "on_list_keyDown: %d\n" % event.keyCode
        event.skip()
    """

    def on_btnConvert_mouseClick(self, event):
            
        def C2F(): #currentItem == 0
            a = self.components.tfInput.text
            b = (9.0 / 5.0) * float(a) + 32
            self.components.tfOutput.text = str(b)
        def F2C():
            a = self.components.tfInput.text
            b = (5.0 / 9.0) * (float(a) - 32)
            self.components.tfOutput.text = str(b)

        def Km2Mi(): #currentItem == 1
            a = self.components.tfInput.text
            b = float(a) * 0.621371192
            self.components.tfOutput.text = str(b)
        def Mi2Km():
            a = self.components.tfInput.text
            b = float(a) * 1.609344
            self.components.tfOutput.text = str(b)

        def M2Ft(): #currentItem == 2
            a = self.components.tfInput.text
            b = float(a) * 3.2808399
            self.components.tfOutput.text = str(b)
        def Ft2M():
            a = self.components.tfInput.text
            b = float(a) * 0.3048
            self.components.tfOutput.text = str(b)

        def Cm2In(): #currentItem == 3
            a = self.components.tfInput.text
            b = float(a) * 0.393700787
            self.components.tfOutput.text = str(b)
        def In2Cm():
            a = self.components.tfInput.text
            b = float(a) * 2.54
            self.components.tfOutput.text = str(b)

        def Kg2Lb(): #currentItem == 4
            a = self.components.tfInput.text
            b = float(a) * 2.20462262
            self.components.tfOutput.text = str(b)
        def Lb2Kg():
            a = self.components.tfInput.text
            b = float(a) * 0.45359237
            self.components.tfOutput.text = str(b)

        def L2Gal(): #currentItem == 5
            a = self.components.tfInput.text
            b = float(a) * 0.264172052
            self.components.tfOutput.text = str(b)
        def Gal2L():
            a = self.components.tfInput.text
            b = float(a) * 3.78541178
            self.components.tfOutput.text = str(b)

        def Cl2Oz(): #currentItem == 6
            a = self.components.tfInput.text
            b = float(a) * 0.338140227
            self.components.tfOutput.text = str(b)
        def Oz2Cl():
            a = self.components.tfInput.text
            b = float(a) * 2.95735296
            self.components.tfOutput.text = str(b)

        def Kmh2Mph(): #currentItem == 7
            a = self.components.tfInput.text
            b = float(a) * 0.621
            self.components.tfOutput.text = str(b)
        def Mph2Kmh():
            a = self.components.tfInput.text
            b = float(a) * 1.609
            self.components.tfOutput.text = str(b)

        def Kmh2Kt(): #currentItem == 8
            a = self.components.tfInput.text
            b = float(a) * 0.539956803
            self.components.tfOutput.text = str(b)
        def Kt2Kmh():
            a = self.components.tfInput.text
            b = float(a) * 1.852
            self.components.tfOutput.text = str(b)



        if self.components.list.GetItemText(self.currentItem) == 'Celsius':
            if self.components.chkReverse.checked:
                F2C();
            else:
                C2F();
                
        if self.components.list.GetItemText(self.currentItem) == 'Kilometers':
            if self.components.chkReverse.checked:
                Mi2Km();
            else:
                Km2Mi();

        if self.components.list.GetItemText(self.currentItem) == 'Meters':
            if self.components.chkReverse.checked:
                Ft2M();
            else:
                M2Ft();

        if self.components.list.GetItemText(self.currentItem) == 'Centimeters':
            if self.components.chkReverse.checked:
                In2Cm();
            else:
                Cm2In();

        if self.components.list.GetItemText(self.currentItem) == 'Kilos':
            if self.components.chkReverse.checked:
                Lb2Kg();
            else:
                Kg2Lb();

        if self.components.list.GetItemText(self.currentItem) == 'Liters':
            if self.components.chkReverse.checked:
                Gal2L();
            else:
                L2Gal();

        if self.components.list.GetItemText(self.currentItem) == 'Centiliters':
            if self.components.chkReverse.checked:
                Oz2Cl();
            else:
                Cl2Oz();

        if self.components.list.GetItemText(self.currentItem) == 'Kilometers/h':
            if self.components.chkReverse.checked:
                Mph2Kmh();
            else:
                Kmh2Mph();

        if self.components.list.GetItemText(self.currentItem) == 'Kilometers/h':
            if self.components.chkReverse.checked:
                Kt2Kmh();
            else:
                Kmh2Kt();

        
        else:
            pass



if __name__ == '__main__':
    app = model.Application(ConvApp);
    app.MainLoop();


